//
//  KFDAlarmRingtoneSettingsViewControllerTableViewController.m
//  KFD
//
//  Created by Lucky Luke on 7/30/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDAlarmRingtoneSettingsViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDSettingsManager.h"
#import "JTToast.h"
#import "JTToastSettings.h"

@interface KFDAlarmRingtoneSettingsViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *upSoundSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *downSoundSwitch;
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
@property (weak, nonatomic) IBOutlet UISwitch *vibrationSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *timeIntervalSwitch;
@property (weak, nonatomic) IBOutlet UILabel *startTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;

@property (strong,nonatomic) NSNumber* duration;

@end

@implementation KFDAlarmRingtoneSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"预警铃声设置";
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    KFDSettingsManager* settingsManager=[KFDSettingsManager sharedManager];
    [settingsManager getAlarmRingtoneSettingsWithSuccessHandler:^(KFDAlarmRingtoneSettings *settings) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.upSoundSwitch setOn:settings.up_sound];
            [self.downSoundSwitch setOn:settings.down_sound];
            self.vibrationSwitch.on=settings.shock;
            self.durationLabel.text=[NSString stringWithFormat:@"%d秒",settings.notice_time.intValue];
            self.timeIntervalSwitch.on=settings.istime;
            self.startTimeLabel.text=settings.stime;
            self.endTimeLabel.text=settings.etime;
        });
    } failureHandler:^(KFDResponseBody *response) {
        
    }];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    KFDSettingsManager* settingsManager=[KFDSettingsManager sharedManager];
    settingsManager.alarmRingtoneSettings.up_sound=self.upSoundSwitch.isOn;
    settingsManager.alarmRingtoneSettings.down_sound=self.downSoundSwitch.isOn;
    settingsManager.alarmRingtoneSettings.shock=self.vibrationSwitch.isOn;
    settingsManager.alarmRingtoneSettings.notice_time=self.duration;
    settingsManager.alarmRingtoneSettings.stime=self.startTimeLabel.text;
    settingsManager.alarmRingtoneSettings.etime=self.endTimeLabel.text;
    [settingsManager updateAlarmRingtoneSettingsWithSuccessHandler:^{
        
    } failureHandler:^(KFDResponseBody *response) {
        
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 2:
        {
            UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"请输入持续时间(秒)" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                
            }];
            UIAlertAction* cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:cancel];
            UIAlertAction* confirm=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                UITextField* input=alertController.textFields[0];
                int period=input.text.floatValue;
                if (period>60) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[JTToast makeText:@"最长不超过60秒" gravity:JTToastGravityCenter] showInView:self.view size:CGSizeMake(160,40)];
                    });
                }else{
                    self.duration=@(period);
                    self.durationLabel.text=[NSString stringWithFormat:@"%d秒",period];
                }
            }];
            [alertController addAction:confirm];
            [self presentViewController:alertController animated:YES completion:nil];
            break;
        }
        case 6:
        {
//            UIDatePicker* timePicker=[[UIDatePicker alloc] init];
//            timePicker.datePickerMode=UIDatePickerModeTime;
//            [self.view addSubview:timePicker];
        }
        case 7:
        {
            
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end